//
//  OrdinaryLoginController.m
//  ShanYanSDk
//
//  Created by 白红光 on 2018/6/9.
//  Copyright © 2018年 白红光. All rights reserved.
//

#import "OrdinaryLoginController.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import "ViewController.h"
@interface OrdinaryLoginController ()
@property(nonatomic,weak) UITextField * iphoneField;
@property(nonatomic,weak) UITextField * codeField;
@end

@implementation OrdinaryLoginController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:false];
    self.navigationController.navigationBar.hidden = NO;
    self.iphoneField.text = self.iphoneNum;
    
    [self backBtn];
    
}

- (void)backBtn {
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [backButton setImage:[UIImage imageNamed:@"title_back.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backItemClick {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:true];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"验证码登录";
    
    [self setupUI];
    
    if (self.str.length > 0) {
        [self showMessage:self.str];
    }
    
}

- (void)setupUI {
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    
    UIImageView * topImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矢量智能对象"]];
    [self.view addSubview:topImg];
    [topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.offset(70*WIDTHRADIU);
        make.width.offset(56*WIDTHRADIU);
        make.height.offset(28*WIDTHRADIU);
        
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = RGBCOLOR(82,91,112);
    titleLabel.text = @"请登录";
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f*WIDTHRADIU];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(43*WIDTHRADIU);
        make.top.equalTo(topImg.mas_bottom).offset(51*WIDTHRADIU);
        
    }];
    
    UITextField * iphoneField = [[UITextField alloc] init];
    iphoneField.placeholder = @"请输入手机号";
    iphoneField.font = [UIFont systemFontOfSize:15.f*WIDTHRADIU];
    [self.view addSubview:iphoneField];
    [iphoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(37*WIDTHRADIU);
        make.right.offset(-32*WIDTHRADIU);
        make.top.equalTo(titleLabel.mas_bottom).offset(25*WIDTHRADIU);
    }];
    self.iphoneField = iphoneField;
    
    UIView * firstLine = [[UIView alloc] init];
    firstLine.backgroundColor = RGBCOLOR(205,208,212);
    [self.view addSubview:firstLine];
    [firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iphoneField.mas_bottom).offset(13*WIDTHRADIU);
        make.left.offset(37*WIDTHRADIU);
        make.right.offset(-38*WIDTHRADIU);
        make.height.offset(1*WIDTHRADIU);
    }];
    
    UITextField * codeField = [[UITextField alloc] init];
    codeField.placeholder = @"请输入验证码";
    codeField.font = [UIFont systemFontOfSize:15.f*WIDTHRADIU];
    [self.view addSubview:codeField];
    [codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(37*WIDTHRADIU);
        make.width.offset(145*WIDTHRADIU);
        make.top.equalTo(firstLine.mas_bottom).offset(28*WIDTHRADIU);
        
    }];
    self.codeField = codeField;
    
    UIView * secondLine = [[UIView alloc] init];
    secondLine.backgroundColor = RGBCOLOR(205,208,212);
    [self.view addSubview:secondLine];
    [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(codeField.mas_bottom).offset(13*WIDTHRADIU);
        make.left.offset(37*WIDTHRADIU);
        make.width.offset(145*WIDTHRADIU);
        make.height.offset(1*WIDTHRADIU);
        
    }];
    
    // 获取验证码
    UIButton * OrdinaryLogincodeBtn = [UIButton new];
    [OrdinaryLogincodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [OrdinaryLogincodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [OrdinaryLogincodeBtn.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [OrdinaryLogincodeBtn setBackgroundColor:RGBCOLOR(78,132,255)];
    [self.view addSubview:OrdinaryLogincodeBtn];
    [OrdinaryLogincodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(codeField.mas_right).offset(10*WIDTHRADIU);
        make.bottom.equalTo(secondLine.mas_bottom);
        make.width.offset(145*WIDTHRADIU);
        make.height.offset(40*WIDTHRADIU);
        
    }];
    [OrdinaryLogincodeBtn addTarget:self action:@selector(OrdinaryLoginCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [OrdinaryLogincodeBtn.layer setMasksToBounds:YES];
    [OrdinaryLogincodeBtn.layer setCornerRadius:3.0];
    
    // 登录
    UIButton * OrdinaryLoginBtn = [UIButton new];
    [OrdinaryLoginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [OrdinaryLoginBtn setTintColor:[UIColor whiteColor]];
    [OrdinaryLoginBtn.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [OrdinaryLoginBtn setBackgroundColor:RGBCOLOR(78,132,255)];
    [self.view addSubview:OrdinaryLoginBtn];
    [OrdinaryLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(secondLine.mas_bottom).offset(57*WIDTHRADIU);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(300*WIDTHRADIU);
        make.height.offset(50*WIDTHRADIU);
        
    }];
    [OrdinaryLoginBtn addTarget:self action:@selector(OrdinaryLoginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [OrdinaryLoginBtn.layer setMasksToBounds:YES];
    [OrdinaryLoginBtn.layer setCornerRadius:3.0];
    
    
}

// 验证码按钮
- (void)OrdinaryLoginCodeBtnClick:(UIButton *)btn {
    
    if (self.iphoneField.text.length == 11) {
        
        [self openCountdown:btn];
        int y =1000 +  (arc4random() % 9000);
        
        self.codeField.text = [NSString stringWithFormat:@"%d",y];
    }else {
        [self showMessage:@"请输入手机号"];
    }
    
}

// 登录按钮
- (void)OrdinaryLoginBtnClick {
    if (self.codeField.text.length == 4) {
        
        for (UIViewController *controller in self.navigationController.viewControllers) {
            
            if ([controller isKindOfClass:[ViewController class]]) {
                
                ViewController *vc = (ViewController *)controller;
                vc.result = self.iphoneField.text;
                [self.navigationController popToViewController:vc animated:YES];
            }
        }
    }else {
        [self showMessage:@"请输入验证码"];
    }
}

// 开启倒计时效果
-(void)openCountdown:(UIButton *)btn{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                // 设置按钮的样式
                [btn setTitle:@"重新发送" forState:UIControlStateNormal];
                [btn setBackgroundColor:RGBCOLOR(78,132,255)];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.userInteractionEnabled = YES;
                
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [btn setTitle:[NSString stringWithFormat:@"再次获取（%.2d）", seconds] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn setBackgroundColor:RGBCOLOR(194,196,201)];
                btn.userInteractionEnabled = NO;
                
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

// 吐丝
- (void)showMessage:(NSString *)msg{
    if ([msg isEqualToString:@""]) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = [UIFont systemFontOfSize:18.f];
    hud.detailsLabel.backgroundColor = [UIColor clearColor];
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2.0];
    
}

@end
